import 'package:flutter/material.dart';
import 'package:picker_select/data_picker/widget/date_picker.dart';

import 'bottom.dart';

///***********************************************
///文件描述：
///创建人：dgg108698
///创建时间：2020/10/26
///修改人：
///修改时间：
///修改备注：
///***********************************************

class _DatePickerDialog extends StatefulWidget {
  final DateTime initialDateTime;
  final DateTime minimumDate;
  final DateTime maximumDate;
  final List<DggDateUnit> unitList;

  _DatePickerDialog({
    this.initialDateTime,
    this.unitList,
    this.maximumDate,
    this.minimumDate,
  });

  @override
  State<StatefulWidget> createState() => _DatePickerDialogState();
}

class _DatePickerDialogState extends State<_DatePickerDialog> {
  DateTime dateTime;

  @override
  void initState() {
    super.initState();
    dateTime = widget.initialDateTime ?? DateTime.now();
  }

  @override
  Widget build(BuildContext context) {
    return CpsBottomDialog.form(
      title: Text(
        '选择时间',
        style: TextStyle(
            color: Color(0xFF202020),
            fontSize: 16,
            fontWeight: FontWeight.bold),
      ),
      contentPadding: EdgeInsets.all(0),
      positive: Icon(Icons.cancel_outlined, size: 25, color: Color(0xFF999999)),
      negative: Container(),
      onTapPositive: () {
        Navigator.pop(context);
      },
      onTapNegative: () {
        Navigator.pop(context);
      },
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          DggDatePicker(
            unitList: widget.unitList,
            maximumDate: widget.maximumDate,
            minimumDate: widget.minimumDate,
            onDateTimeChanged: (value) {
              dateTime = value;
            },
            initialDateTime: dateTime,
          ),
          Container(
            margin: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
            width: double.infinity,
            height: 40,
            child: FlatButton(
              padding: EdgeInsets.zero,
              color: Color(0xFFFFD433),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              onPressed: () {
                Navigator.pop(context, dateTime);
              },
              child: Text(
                "确定",
                style: TextStyle(
                    color: Color(0xFF202020),
                    fontSize: 18,
                    fontWeight: FontWeight.normal),
              ),
            ),
          )
        ],
      ),
    );
  }
}

Future<DateTime> cpsShowBottomDatePicker({
  @required BuildContext context,
  @required List<DggDateUnit> unitList,
  DateTime initialDateTime,
  DateTime minimumDate,
  DateTime maximumDate,
}) {
  return cpsShowBottomDialog(
      context: context,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10),
            topRight: Radius.circular(10)),
      ),
      builder: (context) {
        return _DatePickerDialog(
          unitList: unitList,
          initialDateTime: initialDateTime,
          minimumDate: minimumDate,
          maximumDate: maximumDate,
        );
      });
}
